package com.Set;

import java.util.*;

/**
 * Colletions工具类的常用API
 */

public class CollectionsDemo {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        Set<String> set=new TreeSet<>();
//        list.add("java");
//        list.add("python");
//        list.add("scala");
//        list.add("Spark");
//        list.add("SpringBoot");

        /**
         * 1.批量添加元素方法（addAll）
         * 第一个参数是要添加到的集合，第二个是添加的内容
         */
        Collections.addAll(list,"java","python","scaa","Spark","SpringBoot");
        System.out.println(list);
        //[java, python, scaa, Spark, SpringBoot]

        //Set集合也可以批量添加元素
        Collections.addAll(set,"java","python","scaa","Spark","SpringBoot");
        System.out.println(set);
        //[Spark, SpringBoot, java, python, scaa]


        /**
         * 2.打乱List集合顺序(shuffle)
         * set不行，set本来就是无序的
         */
        Collections.shuffle(list);
        System.out.println(list);
        //[java, python, scaa, Spark, SpringBoot]
        //[SpringBoot, scaa, Spark, java, python]



        /**
         * 3.将List集合中的元素按照默认规则排序
         * 数字就是升序，字符串就是按照首字母的ASCII码大小进行盘排序。数字 < 大写字母 < 小写字母 < 文字
         */
        Collections.sort(list);
        System.out.println(list);

        /**
         * 4.将自定义类型对象集合按照自定义规定排序
         */
        List<Apple> apple=new ArrayList<>();
        Apple apple1=new Apple("青苹果",100,12.0);
        Apple apple2=new Apple("红苹果",120,18.0);
        Apple apple3=new Apple("黄苹果",60,6.0);
        Apple apple4=new Apple("黑苹果",30,5.0);
        //批量添加元素
        Collections.addAll(apple,apple1,apple2,apple3,apple4);
        //1.第一种使类继承Compabe接口，重写copareTo方法
        Collections.sort(apple);
        System.out.println(apple);

        //2.使用Collections自带的比较器Compare
        Collections.sort(apple, new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                //降序排列
                return Double.compare(o2.getPrice(), o1.getPrice());
            }
        });
        System.out.println(apple);

    }
}
